import {App} from "vue";
import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router";
import {RedirectRoute} from "@/router/base";
import {createRouterGuards} from "./router-guards";
import {Layout} from "@/router/constant";
import SystemManagementList from "@/views/SysApp/SysAppList.vue";
import {SysAppEntry} from "@/views/SysApp/index";
import {SysResourceEntry} from "@/views/SysResource/index";
import {SysTbDictGroupEntry} from "@/views/SysTbDictGroup/index";
import {SysTbDictEntry} from "@/views/SysTbDict/index";
import Login from "@/views/login/Login.vue";
import {SysOperateLogEntry} from "@/views/SysOperateLog";
import LoginContainer from "@/views/LoginContainer/index.vue";
import {SysTbSecurityObjEntry} from '@/views/SysTbSecurityObj'
// const modules = import.meta.globEager("./modules/**/*.ts");

const routeModuleList: RouteRecordRaw[] = [];

// Object.keys(modules).forEach((key) => {
//   const mod = modules[key].default || {};
//   const modList = Array.isArray(mod) ? [...mod] : [mod];
//   routeModuleList.push(...modList);
// });

function sortRoute(a, b) {
  return (a.meta?.sort || 0) - (b.meta?.sort || 0);
}

routeModuleList.sort(sortRoute);

export const RootRoute: RouteRecordRaw = {
  path: "/",
  name: "Root",
  redirect: "/login",
  meta: {
    title: "Root"
  }
};

export const LoginRoute: RouteRecordRaw = {
  path: "/login",
  name: "Login",
  component: LoginContainer,
  meta: {
    title: "登录"
  }
};
export const Navigation: RouteRecordRaw[] = [
  {
    path: "/SysApp/SysTbDictList",
    name: "ttt",
    component: Login,
    meta: {
      title: "登录"
    }
  },
  {
    path: "/SysApp/SysTbSecurityObjEntry",
    name: "SysApp-SysTbSecurityObjEntry",
    component: SysTbSecurityObjEntry,
    meta: {
      title: "系统数据资源配置"
    }
  },
  {
    path: "/SysApp/SystemManagementList",
    name: "SysApp-SystemManagementList",
    component: SystemManagementList,
    meta: {
      title: "系统管理"
    }
  },
  {
    path: "/SysApp/SysResourceEntry",
    name: "SysApp-SysResourceEntry",
    component: SysResourceEntry,
    meta: {
      title: "系统资源配置"
    }
  },
  {
    path: "/SysApp/SysAppEntry",
    name: "SysApp-SysAppEntry",
    component: SysAppEntry,
    meta: {
      title: "系统资源配置"
    }
  },
  {
    path: "/SysApp/SysTbDictEntry",
    name: "SysApp-SysTbDictEntry",
    component: SysTbDictEntry,
    meta: {
      title: "字典管理",
    }
  },
  {
    path: "/SysApp/SysOperateLog/SysOperateLogEntry",
    name: "SysApp-SysOperateLog",
    component: SysOperateLogEntry,
    meta: {
      title: "业务日志"
    }
  },
  {
    path: "/SysApp/SysTbDictGroupEntry",
    name: "SysApp-SysTbDictGroupEntry",
    component: SysTbDictGroupEntry,
    meta: {
      title: "字典组管理"
    }
  },
  {
    path: "/navigation",
    name: "navigation",
    component: Layout,
    children: [
      {
        path: "/welcome",
        name: "Welcome",
        component: SysTbDictGroupEntry,
        meta: {
          title: "欢迎页面"
        }
      }
    ]
  }];
//需要验证权限
export const asyncRoutes = [...routeModuleList];

//普通路由 无需验证权限
export const constantRouter: any[] = [...Navigation, LoginRoute, RootRoute, RedirectRoute];

const router = createRouter({
  history: createWebHashHistory(),
  routes: [...constantRouter],
  scrollBehavior(to: any, from: any) {
    if (to.path !== from.path) {
      return {top: 0};
    }
  }
});

export async function setupRouter(app: App) {
  await createRouterGuards(router);
  await app.use(router);
  // 创建路由守卫
}

export default router;
